package multiNyID.BehaviorCamera;

/*
 * class MarkerBehaviorNyID
 *
 * A time-based triggered behaviour which uses a detector to
 * update the position of model drawn above markers.
 * --------------------------------------------------------------------------------
 * Based On NyARToolkit The Java edition ARToolKit class library.
 * modified from class NyARMarkerBehavior- Andrew Davison [ad@fivedots.coe.psu.ac.th]
 *
 * @author whe_green@yahoo.co.id
 * Analisis Metode Occlusion Based Pada Augmented Reality
 * Studi Kasus Interaksi dengan Objek Virtual Secara Realtime
 * Menggunakan Gerakan Tangan
 *
 * Institut Teknologi Telkom
 * Fakultas Informatika 2007
 * Bandung Indonesia
 *
 * nyargreen.wordpress.com
 */
import multiNyID.Detector.DetectMarkersNyID;
import java.awt.image.BufferedImage;
import java.util.Enumeration;
import javax.media.Buffer;

import javax.media.j3d.*;
import javax.vecmath.*;

import jp.nyatla.nyartoolkit.jmf.utils.*;
import jp.nyatla.nyartoolkit.java3d.utils.*;

import jp.nyatla.nyartoolkit.NyARException;
import jp.nyatla.nyartoolkit.core.param.NyARParam;
import jp.nyatla.nyartoolkit.core.types.*;




public class MarkersBehaviorNyID extends Behavior
                       implements JmfCaptureListener
{
  private final double FPS = 30.0;  // so executes about 30 times/sec


  private Background bg = null;
  private DetectMarkersNyID detectMarkers;   // the detector for the markers

  private WakeupCondition wakeup;

  private JmfCaptureDevice captureDev;   // captures the camera image
  private J3dNyARRaster_RGB rasterRGB;   // the camera image



  public MarkersBehaviorNyID(NyARParam params, Background bg, DetectMarkersNyID ms)
  {
    super();
    this.bg = bg;
    detectMarkers = ms;

    wakeup = new WakeupOnElapsedTime((int)(1000.0/FPS));
    setSchedulingBounds( new BoundingSphere(new Point3d(), 100.0) );

    initCaptureDevice(params);
  }  // end of NyARMarkersBehavior()



  private void initCaptureDevice(NyARParam params)
  {
    NyARIntSize screenSize = params.getScreenSize();

    try {
      JmfCaptureDeviceList devlist = new JmfCaptureDeviceList();  // get devices
      captureDev = devlist.getDevice(0);   // use the first
      captureDev.setCaptureFormat(screenSize.w, screenSize.h, 30.0f);
      captureDev.setOnCapture(this);

      rasterRGB = new J3dNyARRaster_RGB(params, captureDev.getCaptureFormat());  // create raster

      detectMarkers.createDetector(params, rasterRGB);   // initialise detector

      captureDev.start();
    }
    catch(NyARException e)
    {  System.out.println(e);
       System.exit(1);
    }
  }  // end of initCaptureDevice()



  public void initialize()
  {  wakeupOn(wakeup);  }



  public void processStimulus(Enumeration criteria)
  /* use the detector to update the models on the markers */
  {
    try {
      synchronized (rasterRGB) {
        if (bg != null) {
          rasterRGB.renewImageComponent2D();
//
          BufferedImage buffImg = rasterRGB.getImageComponent2D().getImage();

//
//          Graphics2D g2 = (Graphics2D) buffImg.getGraphics();
//          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//          g2.setPaint(Color.GREEN);
////
//          for (int i = 0; i < buffImg.getWidth(); i += buffImg.getWidth()/4)
//          {
//             Shape line = new Line2D.Float(i, 0, i, buffImg.getHeight());
//             g2.draw(line);
//          }
////
//          for (int i = 0; i < buffImg.getHeight(); i += buffImg.getHeight()/4)
//          {
//            Shape line = new Line2D.Float(0, i, buffImg.getWidth(), i);
//            g2.draw(line);
//          }
//
//          g2.dispose();
//

         
            ImageComponent2D im = new ImageComponent2D(ImageComponent2D.FORMAT_RGB, buffImg);
          bg.setImage(im);

          bg.setImage(rasterRGB.getImageComponent2D());  // refresh background

        }
      }
      detectMarkers.updateModels(rasterRGB);
      wakeupOn(wakeup);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }  // end of processStimulus()



  public void onUpdateBuffer(Buffer buf)
  // triggered by JmfCaptureListener event
  {
    try {
      synchronized (rasterRGB) {
        rasterRGB.setBuffer(buf);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }  // end of onUpdateBuffer()


/*
  public void stop()
  {  captureDev.stop();  }
*/
}  // end of NyARMarkersBehavior class
